package com.example.cofeed.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.blankj.utilcode.util.ObjectUtils;
import com.example.cofeed.adapter.SystemMessagesAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseListActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.entity.MessageNotification;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class SystemMessagesActivity extends BaseListActivity<MessageNotification> {
    private SystemMessagesAdapter mSystemMessagesAdapter;

    @Override
    protected void bindRecyclerView(RecyclerView recLv) {
        setMainTitle("系统消息");

        recLv.setLayoutManager(new LinearLayoutManager(this));
        mSystemMessagesAdapter = new SystemMessagesAdapter();
        recLv.setAdapter(mSystemMessagesAdapter);
        mSystemMessagesAdapter.setOnItemClickListener((adapter, view, position) -> {
            MessageNotification messageNotification = (MessageNotification) adapter.getData().get(position);
            if (ObjectUtils.isNotEmpty(messageNotification.url)){
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 5);
                intent.putExtra(IntentKeyConstant.URL, messageNotification.url);
                startActivity(intent);

            }
        });

    }

    @Override
    protected void loadData(int page, int pageCount) {
        ApiService.message_sysmsg(this, page, new DialogCallback<LzyResponse<List<MessageNotification>>>() {
            @Override
            public void onSuccess(Response<LzyResponse<List<MessageNotification>>> response) {
                onGetDataSuc(response.body().data);
            }

            @Override
            public void onError(Response<LzyResponse<List<MessageNotification>>> response) {
                super.onError(response);
                onGetDataFail();
            }
        });

    }

    @Override
    protected List<MessageNotification> getData() {
        return mSystemMessagesAdapter.getData();
    }

    @Override
    protected void bindData(boolean isRefresh, List<MessageNotification> list) {
        if (isRefresh){
            mSystemMessagesAdapter.setNewData(list);
        }else {
            mSystemMessagesAdapter.addData(list);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
